﻿@model IEnumerable<dynamic>
@{
    ViewBag.Title = EWWebResources.Selection_PageTitle_Index;
    var modelHtmlHelper = new HtmlHelper<EWSelectionDefinition>(this.ViewContext, new EWViewDataContainer<EWSelectionDefinition>() { ViewData = new ViewDataDictionary<EWSelectionDefinition>() });
    modelHtmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix = this.ViewData.TemplateInfo.HtmlFieldPrefix;
}

@(Html.EasyWeb().Grid(this.Model)
      .Name("easyweb-grid")
      .DataKey("Id")
      .Columns(builder => builder.Bound("InternalName", typeof(EWSelectionDefinition).GetDisplayName("InternalName"), true, @<text><a href="@Url.RouteUrl("EasyWeb_Selection", new { action = "Details", id = item.Id })">@item.InternalName</a></text>)
                                 .Bound("DisplayName", typeof(EWSelectionDefinition).GetDisplayName("DisplayName"), false))
      .DataBind(builder => builder.RowCount(this.ViewBag.TotalRowCount))
      .Pageable(builder => builder.PagerMode(EWGridPagerModes.NextPrevious)
                                  .RowsPerPage(20))
      .Sortable(builder => builder.Default("InternalName"))
      .ToHtmlString())

<p><a id="easyweb-action-create" href="javascript:void(0)">@EWWebResources.Selection_Button_Create</a></p>

@section head {
    <style type="text/css">
        #easyweb-action-create {
            margin: 1em 0;
        }
    </style>
}

@section scripts {
    <script type="text/javascript">
        (function ($) {
            EasyWeb.EWTemp.createBegin = function () {
                var form = $("#easyweb-container-create form");
                $.validator.unobtrusive.parse(form);
                return form.validate().form();
            }

            EasyWeb.EWTemp.createSuccess = function (data, status, xhr) {
                var url = $(data).val();
                if (url && url.length > 0) {
                    document.location = url;
                }
            }

            EasyWeb.EWTemp.createFailure = function (xhr, status, error) {
                @EWEasyWeb.MessageDialog(EWWebResources.Window_Text_DefaultError)
            }

            $(function () {
                $("#easyweb-action-create").button().click(function () {
                    $.ajax("@Url.RouteUrl("EasyWeb_Selection", new { action = "Create" })", {
                        cache: false,
                        dataType: "html"
                    }).done(function (html) {
                        html = $("<div id='easyweb-container-create'></div>").append(html);
                        @EWEasyWeb.ModalDialog(EWWebResources.Selection_WindowTitle_Create, @<text>html</text>, @<text>function () { $("#easyweb-container-create form").submit(); return false; }</text>, 460)
                    }).fail(function () {
                        @EWEasyWeb.MessageDialog(EWWebResources.Window_Text_DefaultError)
                    });
                });
            });
        })(jQuery);
    </script>
}